export default{
  namespaced:true,
  state:()=>({
    //{goods_id,goods_name,goods_price,goods_count,goods_small_logo,goods_state}
    cart:uni.getStorageSync('car')?JSON.parse(uni.getStorageSync('car')):[]
  }),
  mutations:{
    addgood(state,goods){
      const a=state.cart.find(x=>x.goods_id===goods.goods_id)
      if(!a){
        
        state.cart.push(goods)
      }else{
        a.goods_count++
      }
      this.commit('m_cat/savestort')
    },
    //gaibianstate
    gaibianstate(state,goods){
      const a=state.cart.find(x=>x.goods_id===goods.goods_id)
      if(a){
        
        a.goods_state=goods.goods_state
        this.commit('m_cat/savestort')
      }
    },
    //修改商品数量
    updatemun(state,goods){
      const a=state.cart.find(x=>x.goods_id==goods.goods_id)
      if(a){
        a.goods_count=goods.goods_count
        this.commit('m_cat/savestort')
      }
    },
    //删除数据
    deleteitem(state,goods){
      state.cart=state.cart.filter(x=>x.goods_id!=goods.goods_id)
      this.commit('m_cat/savestort')
    },
    qubugai(state,a){
      state.cart.forEach(x=>x.goods_state=a)
      
    },
    savestort(state){
      uni.setStorageSync('car',JSON.stringify(state.cart))
    }
  },
  getters:{
    totail(state){
      let c=0
      state.cart.forEach(item=>c=c+item.goods_count)
      
      return c
    },
    zmoney(state){
      let c=0
      state.cart.forEach(item=>{
        if(item.goods_state){
          return c=c+item.goods_price*item.goods_count
        }
      })
      return c
    },
    d1(state){
      const a=state.cart.find(item=>item.goods_state==false)
      if(a) {
        return false
      }else{
        return true
      }
    },
    danshu(state){
      return state.cart.filter(item=>item.goods_state==true).reduce((totle,item)=>totle=totle+item.goods_count,0)
    }
    
  }
  
}